Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls


<ToolboxData("<{0}:SampleValidator runat=server></{0}:Validator>")> _
Public Class SampleValidator
    Inherits WebControls.BaseValidator

    Protected Overrides Function EvaluateIsValid() As Boolean
        Dim cn As New OleDb.OleDbConnection("strConnection")
        Dim cmd As OleDb.OleDbCommand = cn.CreateCommand
        Dim strTestValue As String
        Dim strTestControl As String

        Try
            Me.CheckControlValidationProperty(Me.ControlToValidate, "ControlToValidate")
            strTestControl = Me.ControlToValidate
            strTestValue = Me.GetControlValidationValue(strTestControl)
            cmd.CommandText = "Select count(*) From Table1 Where " & _
                              "KeyValue = '" & strTestValue & "';"
            If cmd.ExecuteScalar = 0 Then
                Return False
            Else
                Return True
            End If
        Catch
        End Try

    End Function

    Protected Overrides Function ControlPropertiesValid() As Boolean
        Dim ctr As System.Web.UI.WebControls.WebControl

        If Me.ControlToValidate = "" Then
            Throw New System.Web.HttpException( _
                 "ControlToValidate property not set for " & Me.UniqueID)
        Else
            If Me.Parent.FindControl(Me.ControlToValidate) Is Nothing Then
                Throw New System.Web.HttpException("Control " & Me.ControlToValidate & _
                    " found for" & Me.ClientID)
            Else
                ctr = Me.Parent.FindControl(Me.ControlToValidate)
                If ctr.GetType().Name <> "TextBox" Then
                    Throw New System.Web.HttpException("Control " & Me.ControlToValidate & _
                          " not a textbox for " & Me.ClientID)
                End If
            End If
        End If

        Return True

    End Function

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        Dim strName As String
        Dim strScript As String
        If Me.DesignMode = False Then
            If Me.DetermineRenderUplevel() And _
               Me.EnableClientScript Then
                writer.AddAttribute("controltovalidate", Me.ControlToValidate)
                writer.AddAttribute("evaluationfunction", "MyControlIsValid")
                writer.AddAttribute("display", Me.Display.ToString())
                writer.AddAttribute("style", "display:none")
                writer.AddAttribute("errormessage", Me.ErrorMessage)

                Me.RegisterValidatorCommonScript()
                Me.RegisterValidatorDeclaration()

                strName = Me.GetControlRenderID(Me.ControlToValidate)

                strScript = "function MyControlIsValid() {if (document.all['" & _
                           strName & "'].length > 2){return true} else {return false};}"
                Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType, _
                                "val" & Me.ControlToValidate, strScript, True)
            End If
        End If
    End Sub

End Class
